package com.sjz;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/****
 * 通过输入的学号来得到姓名
 * @author Administrator
 *
 */
public class MainActivity extends Activity {
	 private EditText studentno;
	    private ServiceConnection conn = new StudentServiceConnection();  //绑定服务需要的参数对象 
	    private IStundent iStundent;
	    private TextView resultView;
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.main);
	        
	        resultView = (TextView) this.findViewById(R.id.resultView);  //得到返回的值的对象
	        studentno = (EditText) this.findViewById(R.id.studentno);//得到输入的内容的值
	        Button button = (Button) this.findViewById(R.id.button);
	        button.setOnClickListener(new ButtonClickListener());  //按钮点击事件 
	        //创建意图对象    显示意图
	        Intent service = new Intent(this, StudentService.class);
	         //BIND_AUTO_CREATE 绑定自动创建服务  值是1
	        bindService(service, conn, BIND_AUTO_CREATE);
	    }
	    
	    private class StudentServiceConnection implements ServiceConnection{
	    	//当客户端绑定服务的时候被调用 
			public void onServiceConnected(ComponentName name, IBinder service) {
				iStundent = (IStundent)service;
			}
			
			
			public void onServiceDisconnected(ComponentName name) {
				iStundent = null;
			}
	    }
	    
	    //当activity被摧毁时  解除绑定  
	    @Override
		protected void onDestroy() {
			unbindService(conn);
			super.onDestroy();
		}

	    /***
	     * 按钮点击事件触发
	     *
	     */
		private final class ButtonClickListener implements View.OnClickListener{
			public void onClick(View v) {
				String no = studentno.getText().toString();  //得到输入的学号
				String name = iStundent.queryStudent(Integer.valueOf(no));//通过学号来查询姓名 
				resultView.setText(name);//把姓名设置进来
			}
	    }
}